package cn.ac.iie.di.ban.jr.rest.util;

import cn.ac.iie.di.ban.jr.rest.handlers.dayhot.DayHotJsongHandler;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Black-Laptop on 8/18/2017.
 */
public class utils {

    public static Logger LOG = Logger.getLogger(utils.class);
    public static String datatype;

    public static List<Object[]> getData(String datatype, JSONObject jsonObject, List<String> colNameList) {
        LOG.info("getData");
        utils.datatype = datatype;
        List<Object[]> objectsList = new ArrayList<>();

        JSONObject sourceJson = JSONObject.parseObject(String.valueOf(jsonObject.getJSONObject("source")));
        if (sourceJson == null) {
            LOG.error(datatype + " no column: sourceJson");
        }
        JSONObject dataJson = JSONObject.parseObject(String.valueOf(sourceJson.getJSONObject("data")));
        if (dataJson == null) {
            LOG.error(datatype + " no column: dataJson");
        }
        Object createTime = dataJson.getString("createtime");
        if (createTime == null) {
            LOG.error(datatype + " no column: createTime");
        }
        for (Object subJson: dataJson.getJSONArray("list")) {
            Object[] objects = getSingleDataDayWeekArry(JSONObject.parseObject(String.valueOf(subJson)), colNameList, createTime);
            objectsList.add(objects);
        }

        return objectsList;
    }

    public static Object[] getSingleDataDayWeekArry(JSONObject jsonObject, List<String> colNameList, Object createTime) {
        List<Object> objectList = new ArrayList<>();
        for (String column: colNameList) {
            LOG.debug("get " + column + " data");
            Object object = null;
            switch (column) {
                case "CREATETIME": {
                    object = createTime;
                    break;
                }
                case "SORTID": {
                    object = jsonObject.getInteger("sortId");
                    if (object == null) {
                        LOG.error(datatype + " no column: sortId");
                    }
                    break;
                }
                case "TITLE": {
//                    object = jsonObject.getString("Title");
//                    if (object == null) {
//                        LOG.error(datatype + " no column: Title");
//                    }
                    String str = jsonObject.getString("Title");
                    if (str == null) {
                        LOG.error("no column: Title");
                    }
                    str = str.replace("\\\\r\\\\n", "");
                    str = str.replace(" ", "+");
                    byte[] ansBytes = Base64.decodeBase64(str);
                    try {
                        object = new String(ansBytes, "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        LOG.error(ExceptionUtils.getFullStackTrace(e));
                    }
                    break;
                }
                case "CONTENTS": {
                    String str = jsonObject.getString("content");
                    if (str == null) {
                        LOG.error(datatype + " no column: content");
                    }
                    str = str.replace("\\\\r\\\\n", "");
                    str = str.replace(" ", "+");
                    byte[] ansBytes = Base64.decodeBase64(str);
                    try {
                        object = new String(ansBytes, "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        LOG.error(ExceptionUtils.getFullStackTrace(e));
                    }
                    break;
                }
                case "URL": {
//                    object = jsonObject.getString("url");
//                    if (object == null) {
//                        LOG.error(datatype + " no column: url");
//                    }
                    String str = jsonObject.getString("url");
                    if (str == null) {
                        LOG.error("no column: url");
                    }
                    str = str.replace("\\\\r\\\\n", "");
                    str = str.replace(" ", "+");
                    byte[] ansBytes = Base64.decodeBase64(str);
                    try {
                        object = new String(ansBytes, "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        LOG.error(ExceptionUtils.getFullStackTrace(e));
                    }
                    break;
                }
                case "PVIEWS": {
                    object = -1;
                    break;
                }
                case "LIKENUM": {
                    object = -1;
                    break;
                }
                case "HOTVALUE": {
                    object = jsonObject.getString("hotValue");
                    if (object == null) {
                        LOG.error(datatype + " no column: hotValue");
                    }
                    break;
                }
                case "HOT_SORT": {
                    String str = jsonObject.getString("type");
                    if (str == null) {
                        LOG.error(datatype + " no column: type");
                    }
                    str = str.replace("\n", "");
                    object = str;
                    break;
                }
                case "ENTERINGTIME": {
                    object = "";
                    break;
                }
            }
            objectList.add(object);
        }
        return objectList.toArray();
    }
}
